package com.game.league.market;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.game.base.app.base.BaseFragment;
import com.game.base.app.callback.ItemOnClick;
import com.game.base.utils.log.Logger;
import com.game.base.view.recyclerview.RecyclerAnimUtil;
import com.game.league.R;
import com.game.league.databinding.FragmentMarketBinding;
import com.game.league.market.adapter.MarketAdapter;
import com.game.league.service.InterfaceMgr;
import com.game.league.task.table.Task;
import com.game.league.util.Constants;
import com.game.league.util.SkipUtil;
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUICenterGravityRefreshOffsetCalculator;
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Su on 2019/6/19.
 */
public class MarketFragment extends BaseFragment<FragmentMarketBinding> implements View.OnClickListener, ItemOnClick {
    private MarketAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_market;
    }

    @Override
    protected void initView() {
        binding.setTitle("每日推荐");
        binding.setRightTitle("我的任务");
        binding.setClick(this);

        initTextBanner();
        binding.recyclerList.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new MarketAdapter(mActivity, this);
        binding.recyclerList.setAdapter(adapter);
        RecyclerAnimUtil.runFallDownAnimation(binding.recyclerList);
        pullRefresh();
    }

    @Override
    protected void initData() {
        InterfaceMgr.getInstance().getTaskList(Constants.TASK_DALY, new FindListener<Task>() {
            @Override
            public void done(List<Task> list, BmobException e) {
                binding.pullToRefresh.finishRefresh();
                if (list == null || list.isEmpty()) return;
                adapter.setItems((ArrayList<Task>) list);
            }
        });
    }

    private void pullRefresh() {
        binding.pullToRefresh.setRefreshOffsetCalculator(new QMUICenterGravityRefreshOffsetCalculator());
        binding.pullToRefresh.setOnPullListener(new QMUIPullRefreshLayout.OnPullListener() {
            @Override
            public void onMoveTarget(int offset) {
            }

            @Override
            public void onMoveRefreshView(int offset) {
            }

            @Override
            public void onRefresh() {
                initData();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_right_tv:
                SkipUtil.showAuditTaskActivity(mActivity);
                break;
        }
    }

    @Override
    public void onItemClick(int viewId, int position, Object o) {
        SkipUtil.showTaskDetailActivity(mActivity, (Task) o);
    }

    private void initTextBanner() {
        List<String> list = InterfaceMgr.getConfig().getTextNotice();
        if (list == null || list.isEmpty()) {
            binding.llBanner.setVisibility(View.GONE);
            return;
        }
        binding.tvBanner.setDatas(list);
        binding.tvBanner.setItemOnClickListener((data, position) -> Logger.d(data + ", " + position));
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            binding.tvBanner.stopViewAnimator();
        } else {
            binding.tvBanner.startViewAnimator();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        binding.tvBanner.stopViewAnimator();
    }

    @Override
    public void onResume() {
        super.onResume();
        binding.tvBanner.startViewAnimator();
    }
}
